package com.cowin.aspect;


import com.alibaba.fastjson.JSONObject;
import com.cowin.base.annotation.InterfaceAnnotation;
import com.cowin.sys.model.TiIfsConfig;
import com.cowin.sys.model.TiIfsEsbInMsg;
import com.cowin.sys.service.TiIfsConfigService;
import com.cowin.sys.service.TiIfsEsbInMsgService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Copyright (C), 1998-2019, 上海亚太计算机信息系统有限公司
 *
 * @author : 于超
 * @Date: 2020/3/30 13:47
 * @Description:
 */
@Slf4j
@Aspect
@Component
@Order(1)
public class InterfaceAspect {

    @Resource
    private TiIfsConfigService tiIfsConfigService;

    @Resource
    private TiIfsEsbInMsgService tiIfsEsbInMsgService;

    @Pointcut("@annotation(com.cowin.base.annotation.InterfaceAnnotation)")
    public void interfacePointCut() {
    }

    @Before("interfacePointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(value = "interfacePointCut()", returning = "ret")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    @Around("interfacePointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {

        Object result = joinPoint.proceed();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method.isAnnotationPresent(InterfaceAnnotation.class)) {
            InterfaceAnnotation interfaceAnnotation = method.getAnnotation(InterfaceAnnotation.class);
            String value = interfaceAnnotation.value();
            Condition condition = new Condition(TiIfsConfig.class);
            condition.createCriteria().andCondition("INTERFACE_ID ='" + value + "'");
            List<TiIfsConfig> byCondition = tiIfsConfigService.findByCondition(condition);
            if (byCondition.size() > 0) {
                TiIfsConfig tiIfsConfig = byCondition.get(0);
                if ("ESB_OUT".equals(tiIfsConfig.getInterfaceComm())) {

                } else if ("ESB_IN".equals(tiIfsConfig.getInterfaceComm())) {
                    TiIfsEsbInMsg tiIfsEsbInMsg = new TiIfsEsbInMsg();
                    tiIfsEsbInMsg.setTiIfsConfigId(tiIfsConfig.getTiIfsConfigId());
                    tiIfsEsbInMsg.setMessageType(tiIfsConfig.getInterfaceDesc());
                    JSONObject jsonObject = JSONObject.parseObject(result.toString());
                    String message = jsonObject.get("message").toString() +"-->"+ jsonObject.get("data").toString();
                    tiIfsEsbInMsg.setReturnMsg(message.length() >= 127 ? message.substring(0, 127) : message);
                    String code = jsonObject.get("code").toString();
                    if ("200".equals(code)) {
                        tiIfsEsbInMsg.setReceiveOk("1");
                    } else {
                        tiIfsEsbInMsg.setReceiveOk("0");
                    }
                    tiIfsEsbInMsg.setReceiveTime(new Date());
                    tiIfsEsbInMsg.setLastUpdateTime(new Date());
                    tiIfsEsbInMsg.setLastUpdateUsername("admin");
                    tiIfsEsbInMsg.setPid(1);
                    CompletableFuture.runAsync(() -> {
                        try {
                            tiIfsEsbInMsgService.save(tiIfsEsbInMsg);
                        } catch (Exception e2) {
                            log.error(e2.getMessage());
                        }
                    });

                }
                tiIfsConfig.setLastUpdateTime(new Date());
                tiIfsConfig.setLastRunningTime(new Date());
                CompletableFuture.runAsync(() -> {
                    try {
                        tiIfsConfigService.update(tiIfsConfig);
                    } catch (Exception e2) {
                        log.error(e2.getMessage());
                    }
                });
            }
        }
        return result;
    }
}
